const { expressjwt: jwt } = require("express-jwt");

module.exports = () => [
  jwt(
    { secret: process.env.JWT_KEY, algorithms: ["HS256"] },
    { maxAge: "2 days" }
  ).unless({
    path: ["/login", "/init"],
  }),
  (error, req, res, next) => {
    if (error.name === "UnauthorizedError") {
      res.status(401).send({
        status: 401,
        message: error.inner.message,
        message: "Invalid token",
      });
    }
  },
  (req, res, next) => {
    // req.decoded = req.auth;
    next();
  },
];
